


// 创建右键菜单
chrome.runtime.onInstalled.addListener(() => {
    // 创建主菜单
    chrome.contextMenus.create({
        "id": "SEO辅助工具",
        "title": "SEO辅助工具",
        "type": "normal",
        "contexts": ["all"]
    });

    // 新窗口打开页面链接
    chrome.contextMenus.create({
        "parentId": "SEO辅助工具",
        "id": "new_window_open",
        "title": "新窗口-打开当前页面",
        "type": "normal",
        "contexts": ["all"]
    });

    // site命令
    chrome.contextMenus.create({
        "parentId": "SEO辅助工具",
        "id": "site_Inquire",
        "title": "site:命令",
        "type": "normal",
        "contexts": ["all"]
    });

    // site:百度收录查询
    chrome.contextMenus.create({
        "parentId": "site_Inquire",
        "id": "site_baidu",
        "title": "site:百度收录查询",
        "type": "normal",
        "contexts": ["all"]
    });

    // site:360收录查询
    chrome.contextMenus.create({
        "parentId": "site_Inquire",
        "id": "site_360",
        "title": "site:360收录查询",
        "type": "normal",
        "contexts": ["all"]
    });

    // site:搜狗收录查询
    chrome.contextMenus.create({
        "parentId": "site_Inquire",
        "id": "site_sogou",
        "title": "site:搜狗收录查询",
        "type": "normal",
        "contexts": ["all"]
    });

    // site:谷歌收录查询
    chrome.contextMenus.create({
        "parentId": "site_Inquire",
        "id": "site_google",
        "title": "site:谷歌收录查询",
        "type": "normal",
        "contexts": ["all"]
    });

    // site:Bing收录查询
    chrome.contextMenus.create({
        "parentId": "site_Inquire",
        "id": "site_bing",
        "title": "site:Bing收录查询",
        "type": "normal",
        "contexts": ["all"]
    });

    // inurl命令
    chrome.contextMenus.create({
        "parentId": "SEO辅助工具",
        "id": "inurl_Inquire",
        "title": "inurl:命令",
        "type": "normal",
        "contexts": ["all"]
    });

    // inurl:百度收录查询
    chrome.contextMenus.create({
        "parentId": "inurl_Inquire",
        "id": "inurl_baidu",
        "title": "inurl:百度收录查询",
        "type": "normal",
        "contexts": ["all"]
    });

    // inurl:360收录查询
    chrome.contextMenus.create({
        "parentId": "inurl_Inquire",
        "id": "inurl_360",
        "title": "inurl:360收录查询",
        "type": "normal",
        "contexts": ["all"]
    });

    // inurl:搜狗收录查询
    chrome.contextMenus.create({
        "parentId": "inurl_Inquire",
        "id": "inurl_sogou",
        "title": "inurl:搜狗收录查询",
        "type": "normal",
        "contexts": ["all"]
    });

    // inurl:谷歌收录查询
    chrome.contextMenus.create({
        "parentId": "inurl_Inquire",
        "id": "inurl_google",
        "title": "inurl:谷歌收录查询",
        "type": "normal",
        "contexts": ["all"]
    });

    // inurl:Bing收录查询
    chrome.contextMenus.create({
        "parentId": "inurl_Inquire",
        "id": "inurl_bing",
        "title": "inurl:Bing收录查询",
        "type": "normal",
        "contexts": ["all"]
    });

    // 第三方平台收录综合查询
    chrome.contextMenus.create({
        "parentId": "SEO辅助工具",
        "id": "three_platform_inquire",
        "title": "第三方平台收录综合查询",
        "type": "normal",
        "contexts": ["all"]
    });

    // 站长工具-SEO收录查询
    chrome.contextMenus.create({
        "parentId": "three_platform_inquire",
        "id": "seo_chinaz",
        "title": "站长工具-SEO收录查询",
        "type": "normal",
        "contexts": ["all"]
    });

    // 5118-SEO收录查询
    chrome.contextMenus.create({
        "parentId": "three_platform_inquire",
        "id": "seo_5118",
        "title": "5118-SEO收录查询",
        "type": "normal",
        "contexts": ["all"]
    });

    // 爱站网-SEO收录查询
    chrome.contextMenus.create({
        "parentId": "three_platform_inquire",
        "id": "seo_aizhan",
        "title": "爱站网-SEO收录查询",
        "type": "normal",
        "contexts": ["all"]
    });

    // Similar-SEO收录查询
    chrome.contextMenus.create({
        "parentId": "three_platform_inquire",
        "id": "seo_similar",
        "title": "Similar-SEO收录查询",
        "type": "normal",
        "contexts": ["all"]
    });

    // 百度小程序查询
    chrome.contextMenus.create({
        "parentId": "SEO辅助工具",
        "id": "xcx_three_platform_inquire",
        "title": "百度小程序查询",
        "type": "normal",
        "contexts": ["all"]
    });

    // 5118-百度小程序查询
    chrome.contextMenus.create({
        "parentId": "xcx_three_platform_inquire",
        "id": "xcx_5118",
        "title": "5118-百度小程序查询",
        "type": "normal",
        "contexts": ["all"]
    });

    // 爱站网-百度小程序查询
    chrome.contextMenus.create({
        "parentId": "xcx_three_platform_inquire",
        "id": "xcx_aizhan",
        "title": "爱站网-百度小程序查询",
        "type": "normal",
        "contexts": ["all"]
    });

    // 指数查询
    chrome.contextMenus.create({
        "parentId": "SEO辅助工具",
        "id": "Indexquery",
        "title": "指数查询",
        "type": "normal",
        "contexts": ["all"]
    });

    // 百度指数查询
    chrome.contextMenus.create({
        "parentId": "Indexquery",
        "id": "index_baidu",
        "title": "百度指数查询",
        "type": "normal",
        "contexts": ["all"]
    });

    // 360指数查询
    chrome.contextMenus.create({
        "parentId": "Indexquery",
        "id": "index_360",
        "title": "360指数查询",
        "type": "normal",
        "contexts": ["all"]
    });

    // 谷歌指数查询
    chrome.contextMenus.create({
        "parentId": "Indexquery",
        "id": "index_google",
        "title": "谷歌指数查询",
        "type": "normal",
        "contexts": ["all"]
    });

    // 下拉联想词-百度
    chrome.contextMenus.create({
        "parentId": "SEO辅助工具",
        "id": "dropdown_baidu",
        "title": "下拉联想词-百度",
        "type": "normal",
        "contexts": ["all"]
    });
});

// 处理右键菜单点击事件
chrome.contextMenus.onClicked.addListener(async (info, tab) => {
    const menuId = info.menuItemId;
    
    try {
        switch (menuId) {
            case "new_window_open":
                const frameUrl = info.frameUrl;
                if (frameUrl) {
                    chrome.tabs.create({ url: frameUrl });
                } else {
                    chrome.tabs.create({ url: info.pageUrl || tab.url });
                }
                break;

            case "site_baidu":
                const url1 = "https://www.baidu.com/s?wd=site:" + getHost(tab.url);
                chrome.tabs.create({ url: url1 });
                break;

            case "site_360":
                const url2 = "https://www.so.com/s?q=site:" + getHost(tab.url);
                chrome.tabs.create({ url: url2 });
                break;

            case "site_sogou":
                const url3 = "https://www.sogou.com/web?query=site:" + getHost(tab.url);
                chrome.tabs.create({ url: url3 });
                break;

            case "site_google":
                const url4 = "https://www.google.com/search?q=site:" + getHost(tab.url);
                chrome.tabs.create({ url: url4 });
                break;

            case "site_bing":
                const url5 = "https://cn.bing.com/search?q=site:" + getHost(tab.url);
                chrome.tabs.create({ url: url5 });
                break;

            case "inurl_baidu":
                const url6 = "https://www.baidu.com/s?wd=inurl:" + getNoHttpUrl(tab.url);
                chrome.tabs.create({ url: url6 });
                break;

            case "inurl_360":
                const url7 = "https://www.so.com/s?q=inurl:" + getNoHttpUrl(tab.url);
                chrome.tabs.create({ url: url7 });
                break;

            case "inurl_sogou":
                const url8 = "https://www.sogou.com/web?query=inurl:" + getNoHttpUrl(tab.url);
                chrome.tabs.create({ url: url8 });
                break;

            case "inurl_google":
                const url9 = "https://www.google.com/search?q=inurl:" + getNoHttpUrl(tab.url);
                chrome.tabs.create({ url: url9 });
                break;

            case "inurl_bing":
                const url10 = "https://cn.bing.com/search?q=inurl:" + getNoHttpUrl(tab.url);
                chrome.tabs.create({ url: url10 });
                break;

            case "seo_chinaz":
                chrome.tabs.create({ url: "http://seo.chinaz.com/" + getHost(tab.url) });
                break;

            case "seo_5118":
                chrome.tabs.create({ url: "https://seo.5118.com/" + getHost(tab.url) });
                break;

            case "seo_aizhan":
                chrome.tabs.create({ url: "https://www.aizhan.com/seo/" + getHost(tab.url) });
                break;

            case "seo_similar":
                chrome.tabs.create({ url: "https://www.similarweb.com/website/" + getHost(tab.url) });
                break;

            case "xcx_5118":
                const selection = info.selectionText;
                if (selection) {
                    chrome.tabs.create({ url: "https://www.5118.com/xcx/" + selection });
                } else {
                    chrome.tabs.create({ url: "https://www.5118.com/xcx/" + tab.title });
                }
                break;

            case "xcx_aizhan":
                chrome.tabs.create({ url: "https://xcx.aizhan.com/" });
                break;

            case "index_baidu":
                const selection1 = info.selectionText;
                if (selection1) {
                    chrome.tabs.create({ url: "http://index.baidu.com/v2/main/index.html#/trend/" + selection1 + "?words=" + selection1 });
                } else {
                    chrome.tabs.create({ url: "http://index.baidu.com/v2/main/index.html#/trend/" + tab.title + "?words=" + tab.title });
                }
                break;

            case "index_360":
                const selection2 = info.selectionText;
                if (selection2) {
                    chrome.tabs.create({ url: "https://trends.so.com/result?query=" + selection2 + "&period=30" });
                } else {
                    chrome.tabs.create({ url: "https://trends.so.com/result?query=" + tab.title + "&period=30" });
                }
                break;

            case "index_google":
                const selection3 = info.selectionText;
                if (selection3) {
                    chrome.tabs.create({ url: "https://trends.google.com/trends/explore?q=" + selection3 });
                } else {
                    chrome.tabs.create({ url: "https://trends.google.com/trends/explore?q=" + tab.title });
                }
                break;

            case "dropdown_baidu":
                const selection4 = info.selectionText;
                if (selection4) {
                    chrome.tabs.create({ url: "http://www.tool90.com/seo/longwords/?sw=" + selection4 });
                } else {
                    chrome.tabs.create({ url: "http://www.tool90.com/seo/longwords/?sw=" + tab.title });
                }
                break;
        }
    } catch (error) {
        console.error("菜单操作失败:", error);
    }
});

// 工具函数
function getHost(url) {
    if (!url) return "";
    const reg = /^http(s)?:\/\/(.*?)\//;
    const match = reg.exec(url);
    return match ? match[2] : "";
}

function getNoHttpUrl(url) {
    if (!url) return "";
    return url.replace("https://", "").replace("http://", "");
}